<?php
/**
 * Cms_Sale
 *
 * @category   Cms
 * @package    Cms_Model
 */
class Cms_Model_Sale extends BIS_Model_Abstract
{

    /**
     * Get Sale by ID
     *
     * @param int $saleId The sale id
     * @return Cms_Resource_Sale_Item|null
     */
    public function getSaleById($saleId)
    {
        $saleId = (int) $saleId;
        return $this->getResource('Sale')->getSaleById($saleId);
    }
    
    /**
     * Get list of Sales
     *
     * @param null|array  $data  sort, order, page, limit, search
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getSales($data = null)
    {
        if ($data) {

			$sortData = array('dateSold','soldBy','total');

            $order = '';
			if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
				$order .= $data['sort'];
			} else {
				$order .= "dateSold";
			}

			if (isset($data['order']) && ($data['order'] == 'desc')) {
				$order .= " DESC";
			} else {
				$order .= " ASC";
			}

            $data['order'] = $order;

			if (isset($data['page'])) {
				if ($data['page'] < 0) {
					$data['page'] = 0;
				}
			}

            if (isset($data['limit'])) {
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
			}else{
                            $data['limit'] = 20;
                        }

			return $this->getResource('Sale')->getSales($data);

		} else {
			return $this->getResource('Sale')->getSales();
			}
    }
    
    /**
     * Get Sale Product by its entryId
     *
     * @param  int $id The id
     * @return Cms_Resource_SaleProduct_Item|null
     */
    public function getSaleProductByEntryId($id)
    {
    	$id = (int) $id;
    
    	return $this->getResource('SaleProduct')->getSaleProductByEntryId($id);
    }

    /**
     * Get Sale Products by its saleId
     *
     * @param int $saleId The sale id to search for
     * @return Zend_Db_Table_Rowset
     */
    public function getSaleProducts($saleId)
    {
        $saleId = (int) $saleId;

        return $this->getResource('SaleProduct')->getSaleProducts($saleId);
    }

     /**
     * Get Total sum of a product in a sale
     *
     * @param int $saleId The sale id to search for
     * @param int $productId The product id to search for in a sale
     * @return int The total product sold
     */
    public function getTotalSaleProductByProductId($saleId, $productId)
    {
        $saleId = (int) $saleId;
        $productId = (int) $productId;
        
        return $this->getResource('SaleProduct')->getTotalSaleProductByProductId($saleId, $productId);
    }

     /**
     * Get Total sum of a product sold
     *
     * @param int  $productId The product id to search for in a sale
     * @return int The Total sum of a product sold
     */
    public function getTotalProductSoldByProductId($productId)
    {
        $productId = (int) $productId;
        
        return $this->getResource('SaleProduct')->getTotalProductSoldByProductId($productId) + $this->getResource('CreditSaleProduct')->getTotalProductSoldByProductId($productId);
    }

    /**
     * Save Sale
     *
     * @param array  $data The data to insert/update
     * @param string $savedBy The full name of the administrator that saved the data
	 * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveSale($data, $savedBy, $row = null)
    {
    	$currentTime = date('Y-m-d H:i:s');

    	if(null !== $row){
    		$data['lastUpdatedBy'] = $savedBy;
    		$data['lastUpdatedDate'] = $currentTime;
    	}else{
    		$data['dateSold'] = $currentTime;
        	$data['soldBy'] = $savedBy;
    	}

        return $this->getResource('Sale')->saveRow($data, $row);
    }

    /**
     * Save Sale Product
     *
     * @param array  $data The data to insert/update
     * @return mixed The primary key
     */
    public function saveSaleProduct($data)
    {
        return $this->getResource('SaleProduct')->saveRow($data);
    }
    
    /**
     * Delete Sale Product
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteSaleProduct($row)
    {
    	return $this->getResource('SaleProduct')->deleteRow($row);
    }

    /**
     * Get sales report
      *
     * @param int|null      $productId  Product id
     * @param string|null   $fromDate	From date sold
	 * @param string|null   $toDate     To date sold
     * @return Zend_Db_Table_Rowset
     */
    public function getSalesReport($productId=null, $fromDate=null, $toDate=null){

        return $this->getResource('SaleProduct')->getSalesReport($productId, $fromDate, $toDate);

    }
    
    /**
     * Get Credit Sale by ID
     *
     * @param int $saleId The sale id
     * @return Cms_Resource_CreditSale_Item|null
     */
    public function getCreditSaleById($saleId)
    {
    	$saleId = (int) $saleId;
    	return $this->getResource('CreditSale')->getCreditSaleById($saleId);
    }
    
    /**
     * Get list of Credit Sales
     *
     * @param null|array  $data  sort, order, page, limit, search
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getCreditSales($data = null)
    {
    	if ($data) {
    
    		$sortData = array('dateSold','soldBy','total');
    
    		$order = '';
    		if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
    			$order .= $data['sort'];
    		} else {
    			$order .= "dateSold";
    		}
    
    		if (isset($data['order']) && ($data['order'] == 'desc')) {
    			$order .= " DESC";
    		} else {
    			$order .= " ASC";
    		}
    
    		$data['order'] = $order;
    
    		if (isset($data['page'])) {
    			if ($data['page'] < 0) {
    				$data['page'] = 0;
    			}
    		}
    
    		if (isset($data['limit'])) {
    			if ($data['limit'] < 1) {
    				$data['limit'] = 20;
    			}
    		}else{
    			$data['limit'] = 20;
    		}
    
    		return $this->getResource('CreditSale')->getCreditSales($data);
    
    	} else {
    		return $this->getResource('CreditSale')->getCreditSales();
    	}
    }
    
    /**
     * Save Credit Sale
     *
     * @param array  $data The data to insert/update
     * @param string $savedBy The full name of the administrator that saved the data
     * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveCreditSale($data, $savedBy, $row = null)
    {
    	$currentTime = date('Y-m-d H:i:s');
    
    	if(null !== $row){
    		$data['lastUpdatedBy'] = $savedBy;
    		$data['lastUpdatedDate'] = $currentTime;
    	}else{
    		$data['dateSold'] = $currentTime;
    		$data['soldBy'] = $savedBy;
    	}
    
    	return $this->getResource('CreditSale')->saveRow($data, $row);
    }
    
    /**
     * Get Credit Sale Product by its entryId
     *
     * @param  int $id The id
     * @return Cms_Resource_CreditSaleProduct_Item|null
     */
    public function getCreditSaleProductByEntryId($id)
    {
    	$id = (int) $id;
    
    	return $this->getResource('CreditSaleProduct')->getCreditSaleProductByEntryId($id);
    }
    
    /**
     * Get Credit Sale Products by its saleId
     *
     * @param int $saleId The sale id to search for
     * @return Zend_Db_Table_Rowset
     */
    public function getCreditSaleProducts($saleId)
    {
    	$saleId = (int) $saleId;
    
    	return $this->getResource('CreditSaleProduct')->getCreditSaleProducts($saleId);
    }
    
    /**
     * Get Total sum of a product in a credit sale
     *
     * @param int $saleId The sale id to search for
     * @param int $productId The product id to search for in a sale
     * @return int The total product sold
     */
    public function getTotalCreditSaleProductByProductId($saleId, $productId)
    {
    	$saleId = (int) $saleId;
    	$productId = (int) $productId;
    
    	return ;
    }
    
    /**
     * Save Credit Sale Product
     *
     * @param array  $data The data to insert/update
     * @return mixed The primary key
     */
    public function saveCreditSaleProduct($data)
    {
    	return $this->getResource('CreditSaleProduct')->saveRow($data);
    }
    
    /**
     * Delete Credit Sale Product
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteCreditSaleProduct($row)
    {
    	return $this->getResource('CreditSaleProduct')->deleteRow($row);
    }
    
}